<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/12/05
 * Time: 15:51
 */
 
 namespace HiCommon\Repository;

use HiCommon\Model\OrderCheckpointRefund;

class OrderCheckpointRefundRepository extends BaseRepository
{
    public function base_model(){
        return new \HiCommon\Model\OrderCheckpointRefund();
    }

    /*用户售后列表*/
    public function get_user_aftersale_list($n_uid){
        $arr_option = [
          'with'=>['vehicle','order_for_user','fail_type']
        ];

        return self::model_child()
                   ->where(
                     function ($query)
                     use ($n_uid) {
                         $n_uid = intval($n_uid);
                         if ($n_uid > 0) {
                             $query->whereHas('order_checkpoint',
                               function ($query)
                               use ($n_uid) {
                                   $query->where('user_id', $n_uid);
                               });
                         }
                     }
                   )
                   ->apipage($arr_option);

    }



    /*用户售后详情*/
    public function get_user_aftersale_detail($n_id){
        $arr_option = [
          'with'=>['vehicle','order_for_user']
        ];
        return self::base_find_one($n_id,$arr_option);

    }

    /*商户售后列表*/
    public function get_shop_aftersale_list($n_shop_id){
        $arr_option = [
          'with'=>['vehicle','order_for_shop','fail_type']
        ];

        $result = self::model_child()
                   ->where(
                     function ($query)
                     use ($n_shop_id) {
                         $n_shop_id = intval($n_shop_id);
                         if ($n_shop_id > 0) {
                             $query->whereHas('order_checkpoint',
                               function ($query)
                               use ($n_shop_id) {
                                   $query->where('shop_id', $n_shop_id);
                               });
                         }
                     }
                   )
          ->whereIn('apply_refund_state',[1,2,3])
          ->orderby('created_at','desc')
                   ->apipage($arr_option);
        return $result;

    }

    /*商户售后详情*/
    public function get_shop_aftersale_detail($n_id){
        $arr_option = [
          'with'=>['vehicle_detail','order_for_shop','fail_type']
        ];
        return self::base_find_one($n_id,$arr_option);

    }

    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all($arr_option = [])
    {
        return OrderCheckpointRefund::commonget($arr_option);
    }

    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list($arr_option = [])
    {
        return OrderCheckpointRefund::commonpage($arr_option);
    }

    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_search_list($arr_where = [],$arr_search = [],$arr_option = [])
    {
        return OrderCheckpointRefund::where($arr_where)
                                    ->where(
                                      function ($query)
                                      use($arr_search) {
                                          $s_key_word = str_trim($arr_search['key_word']);
                                          $s_search_type = str_trim($arr_search['search_type']);
                                          if(mb_strlen($s_key_word) > 0){
                                              switch ($s_search_type)
                                              {
                                                  case 'refund_sn':
                                                      $query->where('refund_sn', 'like', '%'.$s_key_word.'%');
                                                      break;

                                                  case 'plate_no':
                                                      $query->whereHas('njyy_vehicle',
                                                        function ($query)
                                                        use($s_key_word){

                                                            $query->where('plate_no', 'like', '%'.$s_key_word.'%');
                                                        });
                                                      break;
                                                  default:
                                                      //                                                      return [];
                                              }
                                          }

                                          //商家
                                          $n_shop_id = intval($arr_search['shop_id']);
                                          if ($n_shop_id > 0) {
                                              //                    dd($n_shop_id);
                                              $query->whereHas('order_checkpoint',
                                                function ($query)
                                                use ($n_shop_id) {
                                                    $query->where('shop_id', $n_shop_id);
                                                });
                                          }
                                      })
                                    ->commonpage($arr_option);
    }
    
    /**
     * 通过where分页获取数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_by_where($arr_where,$arr_option = [])
    {
        return OrderCheckpointRefund::where($arr_where)->commonpage($arr_option);

    }
    
    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where($arr_where)
    {
        return OrderCheckpointRefund::where($arr_where)->count();
    }
    
    /**
     * 根据条件统计总和
     *
     * @param array  $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_sum_by_where($arr_where = [],$s_field = 'mark')
    {
        return OrderCheckpointRefund::where($arr_where)->sum($s_field);
    }
    
    /**
     * 根据条件批量删除信息
     *
     * @param array $arr_where
     * @param bool  $method
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_by_where($arr_where = [],$method = false)
    {
        return OrderCheckpointRefund::where($arr_where)
                                    ->commondelete($method);
    }
    
    /**
     * 通过where获取一条数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one_by_where($arr_where,$arr_option = [])
    {
        return OrderCheckpointRefund::where($arr_where)->commonfirst($arr_option);
    }

    /**
     * 添加一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        return OrderCheckpointRefund::create($arr_input);
    }
    
    /**
     * 添加多条数据
     *
     * @param array $arr_data
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_many($arr_data)
    {
        return OrderCheckpointRefund::insert($arr_data);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id, $arr_option = [])
    {
        return OrderCheckpointRefund::commonfind($n_id, $arr_option);
    }

    public function edit_by_where($arr_where,$arr_input){
        $res = OrderCheckpointRefund::where($arr_where)->update($arr_input);
        return $res;

    }
    
    /**
     * 更新或新建一条数据
     *
     * @param $arr_where
     * @param $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_or_create($arr_where,$arr_input){
        return OrderCheckpointRefund::updateOrCreate($arr_where,$arr_input);
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        return OrderCheckpointRefund::where('id',$arr_input['id'])->update($arr_input);
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return OrderCheckpointRefund::where('id', $n_id)
                             ->commondelete();
    }

    /**
     * 修改一条数据状态
     *
     * @param int $n_id
     * @param int $n_state
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state(int $n_id,int $n_state)
    {
        return OrderCheckpointRefund::where('id',$n_id)->update(['state'=>$n_state]);

    }
    
    /**
     * 字段自增
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return OrderCheckpointRefund::where('id',$n_id)->increment($s_field,$n_step);
    }

    /**
     * 字段自减
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return OrderCheckpointRefund::where('id',$n_id)->decrement($s_field,$n_step);
    }


}